//
//  MineInfoEditTableViewCell.m
//  MJApp
//
//  Created by scjh on 15/3/27.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineInfoEditTableViewCell.h"

@interface MineInfoEditTableViewCell () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (nonatomic,strong) UIDatePicker *dataPicker;
@property (weak, nonatomic) IBOutlet UISwitch *sexSwitch;

@end

@implementation MineInfoEditTableViewCell

+ (instancetype)initWithTableView:(UITableView *)tableView {
    
    static NSString *cellID = @"MineInfoEditTableViewCell";
    MineInfoEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        
        cell =  WL_XIB(@"MineInfoEditTableViewCell");
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dataPicker = [[UIDatePicker alloc] init];
    }
    cell.cellHigh = CGRectGetHeight(cell.frame);
    return cell;
    
}

- (void)setInfoDic:(NSDictionary *)infoDic {
    
    _infoDic = infoDic;
    self.sexSwitch.hidden = YES;
    NSInteger index = [infoDic[@"index"] integerValue];
    switch (index) {
        case 1: self.textField.keyboardType = UIKeyboardTypeNumberPad;
            break;
        case 6: self.textField.keyboardType = UIKeyboardTypeNumberPad;
            break;
        case 2:
        {
            self.textField.userInteractionEnabled = NO;
            self.sexSwitch.hidden = NO;
            self.sexSwitch.on = [infoDic[@"value"] isEqual:@"男"];
     
        }
            break;
        case 4:
        {
            if (!self.dataPicker) {
                
                
                NSLog(@"sfasfsf");
                self.dataPicker.datePickerMode = UIDatePickerModeDate;
                self.textField.inputView = self.dataPicker;
            }
        }
            break;
        default:
            break;
    }
    self.textField.placeholder = infoDic[@"desc"];
    if ( infoDic[@"value"] ) {
        
        self.textField.text = infoDic[@"value"];
    }
}
- (IBAction)sexSwitchAction:(id)sender {

    UISwitch *switchOn = sender;
    self.textField.text = switchOn.on ? @"男" : @"女";
    [self callBack];
}

- (void)awakeFromNib {
    // Initialization code
    self.textField.delegate = self;

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (BOOL)textField:(UITextField *)textField
  shouldChangeCharactersInRange:(NSRange)range
  replacementString:(NSString *)string {
    
    NSString *allString = nil;
    if ([string isEqual:@""]) {
        
        //删除
        allString = [textField.text substringToIndex:textField.text.length - 1];
    }else {
        //添加
        if (range.length == 0) {
            
            allString = [NSString stringWithFormat:@"%@%@",textField.text,string];
        }else {
            
            allString = textField.text;
        }

    }
    [self callBack];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (textField.inputView) {
        
        textField.text = [NSString stringWithFormat:@"%@",self.dataPicker.date];
        textField.text = [textField.text substringToIndex:10];
        [self callBack];
    }
}

- (void)callBack {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.infoDic];
    [dic setObject:self.textField.text forKey:@"newString"];
    if ([self.delegate respondsToSelector:@selector(homeCell:withInfo:)]) {
        
        [self.delegate homeCell:self withInfo:dic];
    }

}

@end
